// vite.config.js
import { UserConfig, defineConfig } from "vite";
import viteCompression from "vite-plugin-compression";
import vue from "@vitejs/plugin-vue";
import viteSvgIcons from "vite-plugin-svg-icons";
import WindiCSS from "vite-plugin-windicss";

import { resolve } from "path";

export default defineConfig((): UserConfig => {
  return {
    base: "/",
    // root,
    resolve: {
      alias: {
        "@": resolve(__dirname, "src"), //设置别名
      },
    },
    plugins: [
      vue(),
      WindiCSS(),
      viteSvgIcons({
        // 配置路劲在你的src里的svg存放文件
        iconDirs: [resolve(process.cwd(), "src/icons")],
        symbolId: "icon-[dir]-[name]",
      }),
      // 代码压缩
      viteCompression({
        // deleteOriginFile: true, // 压缩后是否删除源文件
      }),
    ],
    server: {
      host: "0.0.0.0",
      port: 8088,
      open: false,
      proxy: {
        "/api": {
          target: "http://127.0.0.1:7002/api/v1",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
        "/img_url": {
          target: "http://127.0.0.1:7002",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/img_url/, ""),
        },
      },
    },
    build: {
      minify: "terser",
      outDir: "www",
      rollupOptions: {
        output: {
          manualChunks: {
            "ant-design-vue": ["ant-design-vue"],
          },
        },
      },
      terserOptions: {
        compress: {
          keep_infinity: true,
          drop_console: true,
        },
      },
      brotliSize: false,
      chunkSizeWarningLimit: 2000,
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "@/assets/styles/global.less";`, // 全局样式
          },
          javascriptEnabled: true,
        },
      },
    },
  };
});
